#!/bin/bash

# 设置输入目录和输出目录
#INPUT_DIR="input"
#OUTPUT_DIR="output"
prefix="test"
picnum=7
#QUALITY="-quality 80 "

# 创建输出目录
#mkdir -p "$OUTPUT_DIR"

# 定义目标尺寸
TARGET_WIDTH=128
TARGET_HEIGHT=160

# 获取所有图像文件（支持 jpg, jpeg, png）
#find "$INPUT_DIR" -type f $ -iname "*.jpg" -o -iname "*.jpeg" -o -iname "*.png" 
for img in "$@"; do
    #basename=$(basename "$img")
    #mainfilename=${basename%.*}
    # 获取图像信息
    width=$(magick identify -auto-orient -format "%w" "$img")
    height=$(magick identify -auto-orient -format "%h" "$img")
    echo "Width:$width Height:$height"
    #converted_img="${mainfilename}shrink".jpg
    converted_img="$prefix$picnum".jpg
    # 判断是否需要旋转
    if [ "$width" -gt "$height" ]; then
        echo "Rotating $img (Width > Height)"
        magick -format JPEG "$img" -auto-orient -rotate 90 -strip -strip -resize "${TARGET_WIDTH}x${TARGET_HEIGHT}!" "$converted_img"
#        magick "$img" -rotate 90 -resize "${TARGET_HEIGHT}x${TARGET_WIDTH}!" "$converted_img"
    else
        echo "No rotation needed for $img"
        magick -format JPEG "$img" -auto-orient -strip -resize "${TARGET_WIDTH}x${TARGET_HEIGHT}!" "$converted_img"
#        magick "$img" -resize "${TARGET_HEIGHT}x${TARGET_WIDTH}!" "$converted_img"
    fi
    let picnum=picnum+1
done

echo "All images processed."
